package edu.fafu.util;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
@Component
public class HibernateSupport {

	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public PageBean getList(String Hql,PageBean pb){
		Query q = getSession().createQuery(Hql);
		ScrollableResults scroll = q.scroll();  
		scroll.last();							
		int totalCount = scroll.getRowNumber() + 1;
		pb.setTotalCount(totalCount);
		q.setMaxResults(pb.getEveryPage());
		q.setFirstResult(pb.getBeginIndex());
		pb.setList(q.list());
		return pb;
	}
	
	public Object getListOne(String Hql){
		Query q = getSession().createQuery(Hql);
		return q.list().get(0);
	}
	
	public void delete(Object o){
		getSession().delete(o);
	}
	
	public void update(Object o){
		getSession().update(o);
	}
	public void add(Object o){
		getSession().save(o);
	}
	
}
